import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n= sc.nextInt();
        sc.nextLine();
        List<String> idcards = new ArrayList<>();
        for (int i = 1; i <= n; i++) {

            idcards.add(sc.nextLine());
        }
        Collections.sort(idcards, Comparator.comparing(idcard -> idcard.substring(6,14)));


        while (sc.hasNext()) {
            String sort = sc.nextLine();
            if (sort.equals("sort1")){
                for(String idcard : idcards) {
                    String year = idcard.substring(6,10);
                    String month = idcard.substring(10,12);
                    String day = idcard.substring(12,14);
                    System.out.println(year + "-" + month + "-" + day);
                }




            }
            else if (sort.equals("sort2")){
                for(String idcard : idcards) {
                    System.out.println(idcard);


                }

            }

            else {
                System.out.println("exit");

            }

        }
        sc.close();
    }


}


